#include "zlgcandevice.h"
#include "controlcan.h"
#include <QDebug>
ZLGCanDevice::ZLGCanDevice( CanDevice* parent ) : CanDevice( parent ) {}
DWORD ZLGCanDevice::CanDeviceOpen( DWORD dwType, DWORD dwIndex, CHAR* pDescription ) {
    Q_UNUSED( pDescription )
    return VCI_OpenDevice( dwType, dwIndex, 0 );
}
DWORD ZLGCanDevice::CanDeviceClose( DWORD dwType, DWORD dwIndex ) {
    return VCI_CloseDevice( dwType, dwIndex );
}

DWORD ZLGCanDevice::CanChannelStart( DWORD dwDeviceHandle, DWORD dwType, DWORD dwIndex, DWORD dwChannel, CanInitConfig* pInitConfig ) {
    Q_UNUSED( dwDeviceHandle )
    VCI_INIT_CONFIG config;
    config.AccCode = pInitConfig->dwAccCode;
    config.AccMask = pInitConfig->dwAccMask;
    config.Filter  = static_cast< UCHAR >( pInitConfig->nFilter );
    config.Mode    = static_cast< UCHAR >( pInitConfig->bMode );
    config.Timing0 = static_cast< UCHAR >( pInitConfig->dwBtr[ 0 ] );
    config.Timing1 = static_cast< UCHAR >( pInitConfig->dwBtr[ 1 ] );
    if ( !VCI_InitCAN( dwType, dwIndex, dwChannel, &config ) ) return false;
    return VCI_StartCAN( dwType, dwIndex, dwChannel );
}

DWORD ZLGCanDevice::CanChannelStopDeviceClose( DWORD dwDeviceHandle, DWORD dwType, DWORD dwIndex, DWORD dwChannel ) {
    Q_UNUSED( dwDeviceHandle )
    Q_UNUSED( dwChannel )
    return CanDeviceClose( dwType, dwIndex );
}
ULONG ZLGCanDevice::CanChannelSend( DWORD dwDeviceHandle, DWORD dwType, DWORD dwIndex, DWORD dwChannel, CanDataFrame* frame, ULONG nCount ) {
    Q_UNUSED( dwDeviceHandle )
    VCI_CAN_OBJ data[ 20 ];
    if ( nCount <= 20 ) {
        for ( ULONG int i = 0; i < nCount; i++ ) {
            data[ i ].ID         = static_cast< UINT >( frame[ i ].uID );
            data[ i ].TimeFlag   = static_cast< BYTE >( frame[ i ].uTimeFlag );
            data[ i ].SendType   = static_cast< BYTE >( frame[ i ].nSendType );
            data[ i ].RemoteFlag = static_cast< BYTE >( frame[ i ].bRemoteFlag );  //是否是远程帧
            data[ i ].ExternFlag = static_cast< BYTE >( frame[ i ].bExternFlag );  //是否是扩展帧
            data[ i ].DataLen    = static_cast< BYTE >( frame[ i ].nDataLen );
            for ( int j = 0; j < data[ i ].DataLen; j++ ) {
                data[ i ].Data[ j ] = static_cast< BYTE >( frame[ i ].arryData[ j ] );
            }
            // memcpy( data[ i ].Data, frame[ i ].arryData, sizeof(frame[i].arryData) );
            //            for ( int k = 0; k < data[ i ].DataLen; k++ )
            //                qDebug() << frame[ i ].arryData[ k ];
        }
        return VCI_Transmit( dwType, dwIndex, dwChannel, data, static_cast< UINT >( nCount ) );
    }
    return 0;
}
ULONG ZLGCanDevice::CanChannelReceive( DWORD dwDeviceHandle, DWORD dwType, DWORD dwIndex, DWORD dwChannel, CanDataFrame* pReceive, ULONG nCount, INT nWaitTime ) {
    Q_UNUSED( dwDeviceHandle )

//    UINT  ret  =  VCI_GetReceiveNum(dwType,dwIndex,dwChannel);
    VCI_CAN_OBJ data[ 200 ];
//    qDebug()<<"dwType:"<<dwType;
//    qDebug()<<"dwIndex:"<<dwIndex;
//    qDebug()<<"dwChannel:"<<dwChannel;
//    qDebug()<<"VCI_GetReceiveNum:"<<ret;
//    ULONG       frameCount = VCI_Receive( dwType, dwIndex, dwChannel, data, 200, 40 );
    ULONG       frameCount = VCI_Receive( dwType, dwIndex, dwChannel, data, static_cast< UINT >( nCount ), nWaitTime );
    for ( ULONG i = 0; i < frameCount; i++ ) {
        pReceive[ i ].uID         = data[ i ].ID;
        pReceive[ i ].uTimeFlag   = data[ i ].TimeFlag;
        pReceive[ i ].nSendType   = data[ i ].SendType;
        pReceive[ i ].bRemoteFlag = data[ i ].RemoteFlag;
        pReceive[ i ].bExternFlag = data[ i ].ExternFlag;
        pReceive[ i ].nDataLen    = data[ i ].DataLen;
        for ( int j = 0; j < data[ i ].DataLen; j++ ) {
            pReceive[ i ].arryData[ j ] = data[ i ].Data[ j ];
        }
        //        memcpy( pReceive[ i ].arryData, data[ i ].Data, 8 );
    }
    return frameCount;
}
